//
//  FallenMenuView.m
//  FallenMenuView
//
//  Created by LuckyStrike on 2017/12/24.
//  Copyright © 2017年 LuckyStrike. All rights reserved.
//

#import "FallenMenuView.h"

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define NAVIGATION_HEIGHT 65
//#define LIST_VIEW_HEIGHT 300

//#define ITEM_SIZE       CGSizeMake(80, 30)
//#define SECTION_INSET UIEdgeInsetsMake(10, 11, 0, 11)



#define ANIMATION_DURATION 0.8f
/** idendify */
#define CELL_ID @"FallenMenuViewCell"
#define HEADER_ID   @"FallenMenuViewHeader"
/** 高度计算 */
#define ITEM_SIZE_HEIGHT 35.0f
#define ITEM_SIZE_WIDTH 80.0f
#define HEADER_HEIGHT 27.0f
#define LINE_SAPCING 10.0f
#define INTERITEM_SPACING 11.0f

#define RGB(r,g,b) [UIColor colorWithRed:r/256.0 green:g/256.0 blue:b/256.0 alpha:1.0]
#define kRGB(k) RGB(k,k,k)

@interface FallenMenuView ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
/** 蒙版  */
@property (nonatomic,strong) UIView *maskView;
/** 列表  */
@property (nonatomic,strong) UICollectionView *listView;
/**   */
@property (nonatomic,copy) SelectedBlock selectedBlock;

@end

@implementation FallenMenuView

#pragma mark -- Private

- (void)updatePrivateLayout {
    self.maskView.backgroundColor = [UIColor colorWithWhite:0 alpha:self.maskAlpha];
    self.listView.backgroundColor = kRGB(244);
    
    /** listViewHeight */
    CGFloat totalHeights = HEADER_HEIGHT * self.sectionTitles.count;
    for (NSArray *items in self.itemTitles) {
        if (items.count == 0) { continue; }
        NSInteger lines = items.count/4 + 1;
        CGFloat lineHeights = lines > 1 ? LINE_SAPCING * (lines - 1) : 0 ;
        CGFloat itemsHeight = ITEM_SIZE_HEIGHT *lines;
        
        totalHeights += (itemsHeight + lineHeights);
    }
    self.listView.frame = CGRectMake(0, -totalHeights, SCREEN_WIDTH, totalHeights);
}

- (void)initPrivate {
    /** 蒙版 */
    self.maskView = [[UIView alloc] initWithFrame:self.bounds];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideMenu)];
    [self.maskView addGestureRecognizer:tap];
    self.maskView.userInteractionEnabled = YES;
    self.maskView.alpha = 0;
    /** 列表 */
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.itemSize = CGSizeMake(ITEM_SIZE_WIDTH, ITEM_SIZE_HEIGHT);
    flow.sectionInset = UIEdgeInsetsMake(10.0, INTERITEM_SPACING, 0, INTERITEM_SPACING);
    flow.minimumLineSpacing = LINE_SAPCING;
    flow.minimumInteritemSpacing = INTERITEM_SPACING;
    self.listView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flow];
    self.listView.scrollEnabled = NO;
    
    [self.listView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CELL_ID];
    [self.listView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HEADER_ID];
    
    self.listView.dataSource = self;
    self.listView.delegate = self;
    
    [self addSubview:self.maskView];
    [self addSubview:self.listView];
}

- (void)setMaskAlpha:(CGFloat)maskAlpha {
    if (_maskAlpha == maskAlpha) { return; }
    _maskAlpha = maskAlpha;
    [self updatePrivateLayout];
}

- (instancetype)initWithSectionTitles:(NSArray*)sectionTitles itemTitles:(NSArray<NSArray*> *)itemTitles selectedBlock:(SelectedBlock)selectedBlock {
    
    if ([super init]) {
        
        _itemTitles = itemTitles;
        _sectionTitles = sectionTitles;
        _selectedBlock = selectedBlock;
        _maskAlpha = 0.3f;
        
        self.frame = CGRectMake(0, NAVIGATION_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATION_HEIGHT);
        
        [self initPrivate];
        [self updatePrivateLayout];
        
        [[UIApplication sharedApplication].keyWindow addSubview:self];
        self.clipsToBounds = YES;
        self.hidden = YES;
    }
    return self;
}

+ (FallenMenuView*)viewWithSectionTitles:(NSArray*)sectionTitles itemTitles:(NSArray<NSArray*> *)itemTitles selectedBlock:(SelectedBlock)selectedBlock {
    return [[FallenMenuView alloc] initWithSectionTitles:sectionTitles itemTitles:itemTitles selectedBlock:selectedBlock];
}

- (void)showMenu {
    
    self.hidden = NO;
    
    [UIView animateWithDuration:ANIMATION_DURATION animations:^{
        
        self.maskView.alpha = 1.0;
        self.listView.frame = CGRectMake(0, 0, SCREEN_WIDTH,self.listView.frame.size.height);
        
    } completion:^(BOOL finished) {
        
    }];
}
- (void)hideMenu {
    
    [UIView animateWithDuration:ANIMATION_DURATION animations:^{
        
        self.maskView.alpha = 0;
        self.listView.frame = CGRectMake(0, -self.listView.frame.size.height, SCREEN_WIDTH, self.listView.frame.size.height);
        
    } completion:^(BOOL finished) {
        
        self.hidden = YES;
        
    }];
    
}

#pragma mark -- UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.sectionTitles.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [[self.itemTitles objectAtIndex:section] count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CELL_ID forIndexPath:indexPath];
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
    lab.backgroundColor = kRGB(232);
    lab.textColor = kRGB(56);
    lab.text = [[self.itemTitles objectAtIndex:indexPath.section] objectAtIndex:indexPath.item];
    lab.font = [UIFont fontWithName:@"PingFangSC-Regular" size:13];
    lab.textAlignment = NSTextAlignmentCenter;
//    lab.layer.borderWidth = 1;
//    lab.layer.borderColor = [UIColor lightGrayColor].CGColor;
    lab.layer.cornerRadius = 5;
    lab.layer.masksToBounds = YES;
    [cell.contentView addSubview:lab];
    
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
        return nil;
    }
    
    UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:HEADER_ID forIndexPath:indexPath];
//    header.backgroundColor = [UIColor lightGrayColor];
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(INTERITEM_SPACING, INTERITEM_SPACING, 200, 13)];
    
    lab.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14];
    lab.text = self.sectionTitles[indexPath.section];
    lab.textColor = kRGB(148);
    [header addSubview:lab];
    
    return header;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(SCREEN_WIDTH, HEADER_HEIGHT);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.selectedBlock(indexPath);
    [self hideMenu];
}

@end
